{literal}
<h2>DomAuth</h2>

DomAuth is quite simple user managment system.<br>
It consists of three classes:<br>
<ul>
	<li>DomUser - main user class</li>
	<li>DomUserActionLog - Logs user actions.</li>
	<li>DomUserGroup - Group. (Not yet finished though)</li>
</ul>

<h2>Setting up user controller</h2>
First you should create your own class from DomUser. This is needed so you can add youre own properties.
<pre class="code">
&lt;?php
class MyUser extends DomUser {
	
	/**
	* @orm plugin DomArDate
	*/
	protected $birthday;

}
?&gt;
</pre>

<pre class="code">
&lt;?php
class UserController extends DomController {
	
	public function index() {
	
	}
	
	public function login() {
		// test if user wants to login
		if( $_REQUEST['username'] && $_REQUEST['password'] ) {
			$result = MyUser::loginByUsername( $_REQUEST['username'], $_REQUEST['password'] );
			
			if( $result === 0) {
				header('location: '.$_SERVER['HTTP_REFERER']);
			} else if( 100 == $result ) {
				$message = 'User not found!';
			} else if( 101 == $result) {
				$message = 'Password is not correct!';
			} else if( 1 == $result) {
				$message = 'Email is not confirmed (user not activated)';
			} else if( 2 == $result) {
				$message = 'User is blocked!';
			} else {
				$message = 'Unknown login error';
			}
						
		}
	}
	
	public function logout() {
		$user = $this->getUser();
		if( $user ) {
			$user->logout();
			header('location: '.$_SERVER['HTTP_REFERER']);
		}
	}
	
	public function register() {
		
		if( $_POST['save'] ) {
			$user = new MyUser();
			$user->username = $_POST['username'];
			$user->password = array($_POST['password'],$_POST['passwordAgain']);
			$user->email = array($_POST['email'],$_POST['emailAgain']);
			
			if( $user->save() ) {
				$message = 'User saved';
				
				$body = 'Hello [$username],
				
				You have been registered.
				Your password is [$password].
				To activate your accound go &lt;a href='.$this->getUrl('confirmRegistration').'[$code]&gt;here&lt;/a&gt;
				';
				
				$user->sendConfirmationEmail('Registration', $body, array('from@mail.ee','From'), $_POST['password'] );
				
				
			} else {
				$message = implode('&lt;br&gt;', $user->getValidationMessages());
			}
		}
		
		$this->view->addPartitioner('contents', 'register.html');
		$this->view->user = $user;
		
	}
	
	public function profile() {
		$user = $this->getUser();
		if( !$user ) return;
		
		
		if( $_POST['changePassword'] ) {
			$user->password = array($_POST['password'],$_POST['passwordAgain'],$_POST['oldPassword']);
			
			if( $user->save() ) {
				$message = 'User saved';
			} else {
				$message = implode('&lt;br&gt;', $user->getValidationMessages());
			}
			
		}
		
		if( $_POST['save'] ) {
			
			$user->username = $_POST['username'];
			$user->email = array($_POST['email'],$_POST['emailAgain']);
			
			if( $user->save() ) {
				$message = 'User saved';
			} else {
				$message = implode('&gt;br&lt;', $user->getValidationMessages());
			}
		}
		
		$this->view->addPartitioner('contents', 'profile.html');
		$this->view->user = $user;
	
	}
	
	public function confirmRegistration( $code ) {
		$action = MyUser::confirmUserEmail( $code );
		if( $action === 0 ) {
			$message = 'User activated';
		} else if( $action == 1){
			$message = 'User not found!';
		} else if( $action == 1){
			$message = 'User already is active!';
		}
	}
	
	public function lostPassword() {
		
		if( $_REQUEST['sendPassword'] ) {
			
			$user = MyUser::getLostPasswordUser( $_REQUEST['email'], $_REQUEST['username'], 'UserM24');
			if( $user ) {
				
				$message  = 'Teie uus parool on [$password]. Kui te olete sisseloginud saate parooli ära muuta! ';
				$user->sendLostPasswordEmail( 'Kaotatud parool', $message, array('from@mail.ee','From') );
				
				$message  = 'New password is sent to your email address';
			} else {
				$message = 'User not found!';
			}
		}
		
	}
	
	
	public function setup() {
		$user = MyUser::getLoggedInUser();
		if( $user )
			$this->getSite()->setUser( $user );
	}


}
?&gt;
</pre>

<h2>User rights</h2>

DomUser has property "rights" that uses DomArAcl. Here are some basic samples:
<pre class="code">
&lt;?php
$user = new MyUser();

// allow user to delete other users.
$user->rights->allow('deleteUsers');
// testing, if is allowed
if( $user->rights->isAllowed('deleteUser') ) {
	// delete some user
}


// you can also give values
$user->rights->allow('seeOwner', SOME_OWNER_ID );
if( $user->rights->getValue('seeOwner') == SOME_OWNER_ID ) {
	// allow
}

?&gt;
</pre>


<h2>Rembember me</h2>

Using DomUser it is very easy to make remember me function:
<pre class="code">
&lt;?php

$loggedInUser; // lets say we have logged in user instance here.

if( $rememberMe ) {
	$loggedInUser->setLoginCookie( 3600*24 ); // remember me for an 24 hours
}

if( $forgetMe ) {
	$loggedInUser->deleteLoginCookie();
}

// Somewhere in the beginning of your code call.
// This will log user in if cookie is set correctly.
MyUser::loginUsingCookie();


?&gt;
</pre>


{/literal}